package com.googlecode.project4uz.tests;

import static org.junit.Assert.*;

import java.beans.Expression;
import java.beans.Statement;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;

import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;

import javax.swing.JButton;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openrdf.rio.RDFFormat;

import com.googlecode.project4uz.demo.BeanTestClass;
import com.googlecode.project4uz.demo.ClassForSerialize;
import com.googlecode.project4uz.demo.RefForSerialize;
import com.googlecode.project4uz.demo.TestHardcoreClass;
import com.googlecode.project4uz.encoder.RDFEncoder;
import com.googlecode.project4uz.encoder.Serializer;

@RunWith(Parameterized.class)
public class RDFEncoderTest {
	private RDFEncoder rdf;

	static Serializer typ;
	Object serializ;
	RDFFormat format;
	String nazwa;
	String ex;

	
	@SuppressWarnings("static-access")
	public RDFEncoderTest(Serializer a, Object b, RDFFormat c, String d,String e) {
		this.typ = a;
		this.serializ = b;
		this.format = c;
		this.nazwa = d;
		ex=e;
		
	}

	@Parameters
	public static Collection<Object[]> prepareConstructor() {

		return Arrays
				.asList(new Object[][] {
					{ Serializer.PUBLIC, new ClassForSerialize(),RDFFormat.RDFXML, "plik01.rdf" ,"liczbaNaturalna"},
					{ Serializer.BEANS, new BeanTestClass(),RDFFormat.RDFXML, "plik02.rdf" ,"getKlasaWew"},
					{ null, new ClassForSerialize(),RDFFormat.RDFXML, "plik03.rdf" ,"liczbaNaturalna"},
					{ null, new BeanTestClass(),RDFFormat.RDFXML, "plik04.rdf" ,"getKlasaWew"},
					{ Serializer.PUBLIC, new TestHardcoreClass(),RDFFormat.RDFXML, "plik05.rdf" ,"tekst_publiczny"},
					{ Serializer.BEANS, new TestHardcoreClass(),RDFFormat.RDFXML, "plik06.rdf" ,"isLogiczna_prywatne"},
					{ Serializer.PUBLIC, new TestHardcoreClass(),RDFFormat.RDFXML, "plik07.rdf" ,"tekst_publiczny_pusta"},
					{ Serializer.BEANS, new TestHardcoreClass(),RDFFormat.RDFXML, "plik08.rdf" ,"getTablica_prywatne_calkowita_pusta"},
					{ Serializer.PUBLIC, null,RDFFormat.RDFXML, "plik09.rdf" ,"tekst_publiczny_pusta"},
					{ Serializer.BEANS, null,RDFFormat.RDFXML, "plik10.rdf" ,"getTablica_prywatne_calkowita_pusta"},
					{ Serializer.PUBLIC, 1,RDFFormat.RDFXML, "plik11.rdf" ,"tekst_publiczny_pusta"},
					{ Serializer.BEANS, 1,RDFFormat.RDFXML, "plik12.rdf" ,"getTablica_prywatne_calkowita_pusta"},
					{ Serializer.PUBLIC, new int [2],RDFFormat.RDFXML, "plik13.rdf" ,"tekst_publiczny_pusta"},
					{ Serializer.BEANS,new int [2],RDFFormat.RDFXML, "plik14.rdf" ,"getTablica_prywatne_calkowita_pusta"},
					{ Serializer.PUBLIC, new TestHardcoreClass(),null, "plik15.rdf" ,"tekst_publiczny_pusta"},
					{ Serializer.BEANS, new TestHardcoreClass(),null, "plik16.rdf" ,"getTablica_prywatne_calkowita_pusta"},
					{ Serializer.PUBLIC, new TestHardcoreClass(),RDFFormat.RDFXML, null ,"tekst_publiczny_pusta"},
					{ Serializer.BEANS, new TestHardcoreClass(),RDFFormat.RDFXML, null ,"getTablica_prywatne_calkowita_pusta"},
					{ Serializer.PUBLIC, new TestHardcoreClass(),RDFFormat.RDFXML, "plik19.rdf" ,null},
					{ Serializer.BEANS, new TestHardcoreClass(),RDFFormat.RDFXML, "plik20.rdf" ,null},
					{ Serializer.PUBLIC, new JButton(),RDFFormat.RDFXML, "plik21.rdf" ,null},
					{ Serializer.BEANS, new JButton(),RDFFormat.RDFXML, "plik22.rdf" ,null},
					{ Serializer.PUBLIC, new ClassForSerialize (),RDFFormat.RDFXML, "plik23.rdf" ,"liczbaRzeczywista"},
					{ Serializer.BEANS, new ClassForSerialize (),RDFFormat.RDFXML, "plik24.rdf" ,"getLiczbaRzeczywista"},
					{ Serializer.PUBLIC, new ClassForSerialize (),RDFFormat.RDFXML, "plik25.rdf" ,"tablica_publiczna_referencji_2"},
					{ Serializer.BEANS, new ClassForSerialize (),RDFFormat.RDFXML, "plik26.rdf" ,"getTablica_prywatne_referencji_2"},
					{ Serializer.PUBLIC, new ClassForSerialize (),RDFFormat.RDFXML, "plik27.rdf" ,"lista_publiczna_rzeczywista"},
					{ Serializer.BEANS, new ClassForSerialize (),RDFFormat.RDFXML, "plik28.rdf" ,"getLista_prywatne_rzeczywista"},
					{ Serializer.PUBLIC, new JButton(),RDFFormat.RDFXML, "plik29.rdf" ,"Class"},
					{ Serializer.BEANS, new JButton(),RDFFormat.RDFXML, "plik30.rdf" ,"getClass"},
					{ Serializer.PUBLIC, new TestHardcoreClass(),RDFFormat.RDFXML, "plik31.rdf" ,"liczbaNaturalna"},
					{ Serializer.BEANS, new TestHardcoreClass(),RDFFormat.RDFXML, "plik32.rdf" ,"getKlasaWew"},	
					{ Serializer.PUBLIC,  new LinkedList <Double> (),RDFFormat.RDFXML, "plik33.rdf" ,"liczbaNaturalna"},
					{ Serializer.BEANS, new LinkedList <Double> (),RDFFormat.RDFXML, "plik34.rdf" ,"getKlasaWew"},	
					{ Serializer.PUBLIC,  new HashMap <Integer, String> (),RDFFormat.RDFXML, "plik35.rdf" ,"liczbaNaturalna"},
					{ Serializer.BEANS, new HashMap <Integer, String> (),RDFFormat.RDFXML, "plik35.rdf" ,"getKlasaWew"},	
					{ Serializer.PUBLIC, new TestHardcoreClass(),RDFFormat.RDFXML, "plik37.rdf" ,"lista_publiczna_referencji"},
					{ Serializer.BEANS, new TestHardcoreClass(),RDFFormat.RDFXML, "plik38.rdf" ,"getLista_prywatne_referencji"},
					{ Serializer.PUBLIC, new TestHardcoreClass(),RDFFormat.RDFXML, "plik39.rdf" ,"mapa_publiczna_int_referencja"},
					{ Serializer.BEANS, new TestHardcoreClass(),RDFFormat.RDFXML, "plik40.rdf" ,"getMapa_prywatne_int_referencja"},
					{ Serializer.PUBLIC, new TestHardcoreClass(),RDFFormat.RDFXML, "plik41.rdf" ,"tablica_publiczna_referencji"},
					{ Serializer.BEANS, new TestHardcoreClass(),RDFFormat.RDFXML, "plik42.rdf" ,"getTablica_prywatne_referencji"},
					{ Serializer.PUBLIC, new TestHardcoreClass(),RDFFormat.RDFXML, "plik43.rdf" ,null},
					{ Serializer.BEANS, new TestHardcoreClass(),RDFFormat.RDFXML, "plik44.rdf" ,null},
				});
	}

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
			rdf = new RDFEncoder(format, typ, new BufferedOutputStream(
					new FileOutputStream(nazwa)));
	}

	@After
	public void tearDown() throws Exception {
		rdf.flush();
		rdf.writeObject(serializ);
		rdf.writeStatement(new Statement(serializ, ex, null));
		rdf.close();
	}

	@Test
	public void testRDFEncoder() {
		assertNotNull("Obiekt nie istnieje", rdf);
	}

	@Test
	public void testGetOwner() {
		ClassForSerialize c = new ClassForSerialize();
		rdf.setOwner(c);
		Object o = rdf.getOwner();
		assertSame("Obiekty nie sa ta sama zmienna", o, c);
	}

	@Test
	public void testSetOwner() {
		rdf.flush();
		ClassForSerialize c = new ClassForSerialize();
		rdf.setOwner(c);
		Object o = rdf.getOwner();
		assertSame("Obiekty nie sa ta sama zmienna", o, c);
	}

	@Test
	public void testWriteObject() {
		rdf.writeObject(serializ);
	}
	
	@Test
	public void testWriteStatementStatement() {
		rdf.writeStatement(new Statement(serializ, ex, null));
	}

	@Test
	public void testWriteExpressionExpression() {
		rdf.writeExpression(new Expression(serializ, ex, null));
		rdf.writeExpression(new Expression (150.5, serializ, "liczbaRzeczywista", null));
	}

	@Test
	public void testWriteComment() {
		rdf.writeComment("Comment");
	}

	@Test
	public void testValueOf() {
		Serializer.valueOf(rdf.toString());
		Serializer.valueOf("PUBLIC");
	}

	@Test
	public void testToString() {
		rdf.toString();
		typ.toString();
		serializ.toString();
	}
	@Test(expected=NullPointerException.class)	
	public void testExpNULL() {
		if(rdf!=null)
			rdf.writeExpression(null);

	}
	@Test(expected=NullPointerException.class)	
	public void testStaNULL() {	
		if(rdf!=null)
			rdf.writeStatement(null);
	}
	
}
